package com.project.ATM;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BankServer {

  Account[] accounts = new Account[3];



  public void setAccounts(Account[] accounts) {
    this.accounts = accounts;
  }

  public BankServer() {
    this.accounts[0] = new Account("100", "000000", 2000);
    this.accounts[1] = new Account("101", "111111", 2000);
    this.accounts[2] = new Account("102", "222222", 2000);
//    System.out.println(Arrays.toString(IDs));
//    System.out.println(Arrays.toString(pwds));
  }

  public void options(int op, int i) {
    switch (op) {
      case 1:
        {
          int money = ATM_UI.saveMoney();
          this.savemoney(money, i);
          break;
        }
      case 2:
        {
          int money = ATM_UI.getMoney();
          this.getmoney(money, i);
          break;
        }
      case 3:
        {
          String toid = ATM_UI.transfer();
          int money = ATM_UI.transferMoney();
          transfer(i, money, toid);
        }
      case 4:
        {
          ATM_UI.quit();
          String ids = ATM_UI.Input_id();
          cheakID(ids);
          return;
        }
      case 5:
        {
          ATM_UI.showoverage(accounts[i]);
          int ops = ATM_UI.welcome();
          options(ops, i);
        }
      default:
        {
          ATM_UI.Show_error_input();
          int ops = ATM_UI.welcome();
          options(ops, i);
          break;
        }
    }
  }

  public void cheakID(String id) {
    for (int i = 0; i < accounts.length; i++) {
      if (accounts[i].getCardno().equals(id)) {
        String input_pwd = ATM_UI.Input_pwd();
        int error_times = 0;
        cheakPWD(input_pwd, i, error_times);
      } else if (i == accounts.length - 1) {
        ATM_UI.Show_error_noId();
        String ids = ATM_UI.Input_id();
        cheakID(ids);
      } else if(id.equals("root")){
        String pwd = ATM_UI.root_input_pwd();
        int error_time =  0;
        cheak_Root_PWD(pwd,error_time);
      }else {
        continue;
      }
    }
  }

  public void cheakPWD(String pwd, int i, int error_times) {
    if (error_times == 2) {
      accounts[i].setStatus(false);
    }
    if (accounts[i].isStatus()) {
      if (accounts[i].getPassword().equals(pwd) && accounts[i].isStatus()) {
        int option = ATM_UI.welcome();
        options(option, i);
      } else {
        String pwd_ = ATM_UI.Show_error_pwd();
        error_times += 1;
        cheakPWD(pwd_, i, error_times);
      }
    } else {
      ATM_UI.Show_error_freeze();
      String new_id = ATM_UI.Input_id();
    }
  }

  public void savemoney(int money, int i) {
    if(money % 100 == 0){
      accounts[i].setMoney(accounts[i].getMoney() + money);
      ATM_UI.saveMoney_ok(accounts[i].getMoney(), i);
      Date now = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String s = sdf.format(now);
      ATM_UI.Printcertificate_save(money,accounts[i],s);
      int option = ATM_UI.welcome();
      options(option, i);
    }
    else{
      ATM_UI.Show_error_NointMoney();
      int option = ATM_UI.welcome();
      options(option, i);
    }
  }

  public void getmoney(int money, int i) {
    if(money % 100 == 0){
      if (money > accounts[i].getMoney()) {
        ATM_UI.Show_error_Nomoney();
        int option = ATM_UI.welcome();
        options(option, i);
      } else {
        accounts[i].setMoney(accounts[i].getMoney() - money);
        ATM_UI.getMoney_ok(accounts[i].getMoney(), i);
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = sdf.format(now);
        ATM_UI.Printcertificate_save(money,accounts[i],s);
        int option = ATM_UI.welcome();
        options(option, i);
      }
    }else{
      ATM_UI.Show_error_NointMoney();
      int option = ATM_UI.welcome();
      options(option, i);
    }

  }

  public void transfer(int i, int money, String to) {
    //    String toid = ATM_UI.transfer();
    for (int j = 0; j < accounts.length; j++) {
      if (accounts[j].getCardno().equals(to)) {
        if (j == i) {
          ATM_UI.Show_error_Notrans_self();
          int option = ATM_UI.welcome();
          options(option, i);
        } else {
          if (money > accounts[i].getMoney()) {
            ATM_UI.Show_error_Nomoney();
            int option = ATM_UI.welcome();
            options(option, i);
          } else {
            accounts[i].setMoney(accounts[i].getMoney() - money);
            accounts[j].setMoney(accounts[j].getMoney() + money);
            ATM_UI.transferMoney_ok();
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String s = sdf.format(now);
            ATM_UI.Printcertificate_transfer(money,accounts[i],s,to);
            int option = ATM_UI.welcome();
            options(option, i);
            break;
          }
        }
      } else if (j == 2) {
        ATM_UI.Show_error_NotFoundUser();
        int option = ATM_UI.welcome();
        options(option, i);
      } else {
        continue;
      }
    }
  }

  public void cheak_Root_PWD(String pwd,int error_time){
    if (pwd.equals(Root.getPassword())){
      int op = ATM_UI.root_options();
      choise_op(op);
    }else{
      String ids = ATM_UI.Input_id();
      cheakID(ids);
    }
  }


  public void choise_op(int op){
    switch (op){
      case 1:{
        String userID = ATM_UI.create_userID();
        String userPWD = ATM_UI.create_userPWD();
        create_newUser(userID,userPWD);
        return;
      } case 2:{
        String userID = ATM_UI.delete_user();
        delete_user(userID);
        return;

      } case 3:{
        String user_before = ATM_UI.change_user_bf();
        String user_after = ATM_UI.change_user_af();
        change_user(user_before,user_after);
        return;
      } case 4:{
        search_user(accounts);
        return;
      } case 5:{
        ATM_UI.quit();
        String ids = ATM_UI.Input_id();
        cheakID(ids);
        return;
      }
    }
  }

  public void create_newUser(String userID,String userPWD){
    Account[] new_arr = new Account[accounts.length+1];
    System.arraycopy(accounts,0,new_arr,0,accounts.length);
    new_arr[new_arr.length-1] = new Account(userID,userPWD,0);
    accounts = new_arr;
    ATM_UI.create_userOK();
    int op = ATM_UI.root_options();
    choise_op(op);
  }

  public void search_user(Account[] accounts) {
    ATM_UI.show_all_user(accounts);
    int op = ATM_UI.root_options();
    choise_op(op);
  }

  public void delete_user(String id){
    int i = 0;
    for (Account a : accounts) {
        if(a.getCardno().equals(id)){
          a.setStatus(false);
          ATM_UI.delete_user_ok();
          int op = ATM_UI.root_options();
      } else if (i == accounts.length-1) {
          ATM_UI.Show_error_noId();
          int op = ATM_UI.root_options();
          choise_op(op);
        }else{
          i += 1;
          continue;
        }
    }
  }

  public void change_user(String id_bf,String id_af){
    int i = 0;
    for (Account a : accounts) {
      if(a.getCardno().equals(id_bf)){
        a.setCardno(id_af);
        ATM_UI.change_user_ok();
        int op = ATM_UI.root_options();
        choise_op(op);
      }else if(i == accounts.length-1){
        ATM_UI.Show_error_noId();
        int op = ATM_UI.root_options();
        choise_op(op);
      }else{
        i += 1;
        continue;
      }
    }
  }



}
